import 'package:flutter/material.dart';

/// 路由操作
class MinRouter {
  GlobalKey<NavigatorState> navigatorKey = GlobalKey();

  /// 返回路由 - pop
  Future navigateBack() async {
    return navigatorKey.currentState.maybePop();
  }

  /// 跳转路由 - pushNamed
  Future navigateTo(String route, {arguments}) async {
    return navigatorKey.currentState.pushNamed('$route', arguments: arguments);
  }

  /// 替换路由 - pushReplacement
  Future replaceTo(String route, {arguments}) async {
    return navigatorKey.currentState.pushReplacementNamed('$route', arguments: arguments);
  }

  // 清栈跳转路由 - pushNamedAndRemoveUntil
  Future clearStackTo(
    String route, {
    arguments,
    Function action,
  }) {
    return navigatorKey.currentState.pushNamedAndRemoveUntil(
      '$route',
      action ?? (Route router) => false,
      arguments: arguments,
    );
  }

  /// 清栈路由 - popUntil
  popUntil({String route = "/"}) {
    navigatorKey.currentState.popUntil(
      ModalRoute.withName('$route'),
    );
  }
}
